package com.tanhua.sso.controller;

import com.tanhua.sso.service.UserService;

import com.tanhua.sso.vo.ErrorResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /***
     * 用户登录
     * @param para
     * @return
     */
    @PostMapping("/loginVerification")
    public ResponseEntity<Object> login(@RequestBody Map<String, String> para) {
        try {
            String phone = para.get("phone");
            String code = para.get("verificationCode");
            String data = this.userService.login(phone, code);

            if (!StringUtils.isEmpty(data)) {
                //登录成功
                String[] split = StringUtils.split(data,"|");
                Map<String, Object> map = new HashMap<>();
                map.put("token", split[0]);
                map.put("isNew", Boolean.valueOf(split[1]));
                return ResponseEntity.ok(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("0000002").errMessage("登录失败").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

}
